package com.eternallove.biz.system.impl;

import com.eternallove.biz.system.SystemDictDataBiz;
import com.eternallove.core.exception.BusinessException;
import com.eternallove.core.constants.ResponseStatus;
import com.eternallove.dao.common.dto.UpdateSortDTO;
import com.eternallove.dao.system.model.SystemDictData;
import com.eternallove.service.system.SystemDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SystemDictDataBizImpl implements SystemDictDataBiz {

    @Autowired
    private SystemDictDataService systemDictDataService;

    @Override
    public Integer create(SystemDictData systemDictData) {
        SystemDictData queryDto = new SystemDictData();
        queryDto.setDictId(systemDictData.getDictId());
        queryDto.setCode(systemDictData.getCode());
        queryDto.setDeleted(Boolean.FALSE);
        SystemDictData dictData = systemDictDataService.findOne(queryDto);
        if (dictData != null) {
            throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "数据值已存在");
        }
        // 排序处理
        queryDto = new SystemDictData();
        queryDto.setDictId(systemDictData.getDictId());
        queryDto.setDeleted(Boolean.FALSE);
        long maxSort = systemDictDataService.count(queryDto);
        systemDictData.setSort(Integer.valueOf("" + maxSort));
        return systemDictDataService.create(systemDictData);
    }

    @Override
    public void updateById(SystemDictData systemDictData) {
        SystemDictData queryDto = new SystemDictData();
        queryDto.setDictId(systemDictData.getDictId());
        queryDto.setCode(systemDictData.getCode());
        queryDto.setDeleted(Boolean.FALSE);
        SystemDictData dictData = systemDictDataService.findOne(queryDto);
        if (dictData != null && !dictData.getId().equals(systemDictData.getId())) {
            throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "数据值已存在");
        }
        systemDictDataService.updateById(systemDictData);
    }

    @Override
    public void sort(UpdateSortDTO dto) {
        SystemDictData dictData = systemDictDataService.findById(dto.getId());
        if (dictData == null || dictData.getDeleted()) {
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(), "排序的数据不存在或已被删除");
        }
        SystemDictData queryDto = new SystemDictData();
        queryDto.setDictId(dictData.getDictId());
        queryDto.setDeleted(Boolean.FALSE);
        List<SystemDictData> dictDataList = systemDictDataService.findList(queryDto);
        int index = 0;
        for (int i = 0; i < dictDataList.size(); i++) {
            if (dictDataList.get(i).getId().equals(dto.getId())) {
                index = i;
                break;
            }
        }
        // 上移
        if ("top".equals(dto.getDirection())) {
            if (index - 1 < 0) {
                return;
            }
            SystemDictData preDictData = dictDataList.remove(index - 1);
            dictDataList.add(index, preDictData);
        }
        // 下移
        else {
            if (index + 1 > dictDataList.size() - 1) {
                return;
            }
            SystemDictData nextDictData = dictDataList.remove(index + 1);
            dictDataList.add(index, nextDictData);
        }
        for (int i = 0; i < dictDataList.size(); i++) {
            dictDataList.get(i).setSort(i);
        }
        // 修改
        systemDictDataService.updateByIdInBatch(dictDataList);
    }
}
